<?php 
namespace town\filter;

use town\base\Filter;
use town\base\Request;
use town\base\Store;

class Purifier implements Filter
{
    /**
     * sql filter
     * @param  Request  $request 请求对象
     * @param  callable $next    链
     * @return 响应对象
     */
    public function doFilter(Request $request, callable $next)
    {
        if (get_magic_quotes_gpc() == false) {
            $this->addslashes_array($_POST);
            $this->addslashes_array($_GET);
            $this->addslashes_array(Store::map());
        }
        return $next($request);
    }


    /**
     * 添加过滤
     * @param  array|string $list 过滤数据
     * @return array
     */
    private function addslashes_array(&$list) {
        if(is_array($list) == false) {
            return addslashes($list);
        }
        foreach($list as $key => &$val){
            $val = $this->addslashes_array($val);
        }
        return $list;
    }
}